cmake_minimum_required(VERSION 3.28)

project(DBLParseSrc)
set(MODULE_NAME ${CMAKE_PROJECT_NAME})
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

set(QT_COMPONENTS
    Widgets
    Network
    WebEngineWidgets
    Svg
    Quick
)
find_package(Qt6 COMPONENTS ${QT_COMPONENTS} REQUIRED)
find_package(Qt6 COMPONENTS LinguistTools)

file(GLOB_RECURSE HEADER_FILES *.h)
file(GLOB_RECURSE SRC_FILES *.cpp)
file(GLOB_RECURSE QRC_FILES *.qrc)
file(GLOB_RECURSE TS_FILES *.ts)
file(GLOB_RECURSE UI_FILES *.ui)
list(REMOVE_ITEM SRC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/core/main.cpp")

add_executable(${MODULE_NAME} MACOSX_BUNDLE WIN32)
target_sources(${MODULE_NAME}
    PRIVATE
        ${SRC_FILES}
        ${HEADER_FILES}
        ${QRC_FILES}
        ${UI_FILES}
        ${TS_FILES})

target_compile_definitions(${MODULE_NAME}
    PRIVATE
        VERSION_STR="${CMAKE_PROJECT_VERSION}"
    PUBLIC
        CMAKE_VERSION="${CMAKE_VERSION}"
        CMAKE_CXX_COMPILER="${CMAKE_CXX_COMPILER}"
        CMAKE_CXX_COMPILER_VERSION="${CMAKE_CXX_COMPILER_VERSION}"
)

list(TRANSFORM QT_COMPONENTS PREPEND Qt${QT_VERSION_MAJOR}::)
# message(STATUS "QT_COMPONENTS ${QT_COMPONENTS}")
target_link_libraries(${MODULE_NAME}
    PRIVATE 
        ${QT_COMPONENTS}
        Pivoter)

if(WIN32)
    configure_file(${PROJECT_SOURCE_DIR}/resources/DBLParse.rc.in 
        ${PROJECT_BINARY_DIR}/DBLParse.rc)
    target_sources(${MODULE_NAME}
        PRIVATE 
            ${PROJECT_BINARY_DIR}/DBLParse.rc)
elseif(APPLE)
    set(MACOSX_ICON_NAME "DBLParse.icns")
    set(MACOSX_ICON_FILE "${CMAKE_SOURCE_DIR}/src/resources/${MACOSX_ICON_NAME}")
    target_sources(DBLParse
        PRIVATE
            ${MACOSX_ICON_FILE})
    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/resources/Info.plist.in
        ${CMAKE_CURRENT_BINARY_DIR}/Info.plist)
    set_target_properties(${MODULE_NAME} 
        PROPERTIES
            MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_BINARY_DIR}/Info.plist")
    set_source_files_properties(${MACOSX_ICON_FILE} PROPERTIES
        MACOSX_PACKAGE_LOCATION "Resources")
elseif(UNIX)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold")
endif()

if (TS_FILES)
    qt_add_translations(${MODULE_NAME} TS_FILES ${TS_FILES})
endif()

if (WIN32)
    set(EXE_DIR ${CMAKE_CURRENT_BINARY_DIR})
    # set(VS_EXE_DIR "${EXE_DIR}/$<$<CONFIG:Release>:Release>$<$<CONFIG:Debug>:Debug>")
    set(EXT_LIBS
        "${CMAKE_SOURCE_DIR}\\libs\\OpenSSL\\libcrypto-1_1-x64.dll"
        "${CMAKE_SOURCE_DIR}\\libs\\OpenSSL\\libssl-1_1-x64.dll")
    add_custom_command(
        TARGET ${MODULE_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E
            copy_if_different ${EXT_LIBS} "${EXE_DIR}")
endif()
